﻿using MesBLL.Common;
using MesDAL.DBInfo;
using MesDAL.DTO.ProcessSheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MesBLL.EventService
{
    public class ProcessSheetService : CommonService<processsheet_log>
    {
        public async Task<List<processsheet_log>> SelectLogListByWorkpieceId(int id)
        {
            return await Task.Run(() => GetAll().Where(x => x.workpiece_id == id).ToList());
        }

        public async Task<List<processsheet_log>> SelectIncompleteProcessSheet()
        {
            return await Task.Run(() => GetAll().Where(x => (x.isfinish == null) || (x.isfinish.Equals(""))).ToList());
        }

        public async Task<processsheet_log> SelectLogByProcessSheetId(int id)
        {
            return await Task.Run(() => GetAll().Where(x => x.id == id).FirstOrDefault());
        }

        public async Task<WorkpieceDTO> SelectWorkpieceByProcessId(int id)
        {
            return await Task.Run(() =>
            {
                WorkpieceDTO workDTO = new WorkpieceDTO();
                var workQury = serverEFModel.processsheet_log.Where(x => x.id == id)
                .Join(serverEFModel.workpiece_info, process => process.workpiece_id, info => info.id, (process, info) => new WorkpieceDTO
                {
                    id = info.id,
                    size = info.size,
                    date = info.date,
                    deadline = info.deadline,
                    material = info.material,
                    name = info.name,
                    qty = info.qty,
                    number = info.number,
                    order = info.order,
                });
                workDTO = workQury.FirstOrDefault();
                return workDTO;
            });
        }
    }
}
